package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.CategorySellVo;
import com.qf.vo.DayStatis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 首页图表
 * @author YuGan
 */
@RestController
@RequestMapping("/admin/dashboard")
@CrossOrigin
@Slf4j
public class DashboardController {
    @Autowired
    private DtsGoodsService dtsGoodsService;
    @Autowired
    private DtsUserService dtsUserService;
    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;
    @Autowired
    private DtsOrderService dtsOrderService;
    @Autowired
    private DtsCategoryService dtsCategoryService;

    @RequestMapping("/")
    public Object dashboard(){
        Integer goodsTotal = dtsGoodsService.findCount();
        Integer userTotal = dtsUserService.findCount();
        Integer productTotal = dtsGoodsProductService.findCount();
        Integer orderTotal = dtsOrderService.findCount();

        Map<String,Object> result = new HashMap<>();
        result.put("goodsTotal",goodsTotal);
        result.put("userTotal",userTotal);
        result.put("productTotal",productTotal);
        result.put("orderTotal",orderTotal);
        return ResponseUtil.ok(result);
    }
    @GetMapping("/chart")
    public Object chart(){

        List<CategorySellAmts> categorySellAmtsList=dtsCategoryService.categorySellAmt();

        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategorySellData(categorySellAmtsList);
        String[] CategoryNames = categorySellAmtsList.stream().map(CategorySellAmts::getName)
                .collect(Collectors.toList())
                .toArray(new String[categorySellAmtsList.size()]);
        categorySellVo.setCategoryNames(CategoryNames);

        //查询日期对应的订单总额
        List<DayStatis> dayStatisList=dtsOrderService.findOrderDayStatis();


        Map<String,Object> result = new HashMap<>();
        result.put("categorySell",categorySellVo);
        result.put("orderAmts",userTotal);
        result.put("userOrderCnt",productTotal);

    }















}